from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from app.exchange import exchange_proxy, check_code


def iqiyi_invoke(country_code, phone_num, phone_code_url, exchange_code):
    proxy_info = exchange_proxy.proxy_method();
    options = webdriver.FirefoxOptions()
    options.add_argument(f'--proxy-server=http://{proxy_info[0]}:{proxy_info[1]}')
    driver = webdriver.Firefox(options=options)
    driver.get("https://vip.iqiyi.com/jihuoma.html")
    driver.maximize_window()

    try:
        time.sleep(1)
        close_tag = driver.find_element(By.CLASS_NAME, 'header__promotion__close')
        driver.execute_script('arguments[0].click();', close_tag)
    except Exception as e:
        print("弹层消失了")

    # 登陆弹窗
    time.sleep(2)
    login_tag = driver.find_element(By.CLASS_NAME, 'J_loginBtn')
    login_tag.click()

    # 账号密码登录
    # login_module = WebDriverWait(driver, 5).until(
    #     EC.presence_of_element_located((By.CSS_SELECTOR, '.field-account>.input-box'))
    # )
    # login_module.send_keys(phone_num)
    #
    # password_input = driver.find_element(By.CLASS_NAME, 'field-confirm-code').find_element(By.CLASS_NAME, 'input-box')
    # password_input.send_keys(check_num)

    # 选择手机号登陆
    time.sleep(1)
    driver.execute_script("document.getElementsByClassName('qy-methods-item')[1].click();")

    # 选择国际区号
    if country_code != '+86':
        time.sleep(1)
        driver.find_element(By.CLASS_NAME, 'field-country-code').click()
        time.sleep(1)
        area_code = country_code.replace('+', '')
        time.sleep(1)
        driver.find_element(By.XPATH, "//li[@data-area-acode='" + area_code + "']").click()

    # 输入手机号
    time.sleep(1)
    phone_input = driver.find_element(By.XPATH, "//input[@data-smsloginreg='nameInput']")
    phone_input.click()
    time.sleep(1)
    phone_input.send_keys(phone_num)

    # 点击发送验证码
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, "send-code-suc").click()

    # 调用接口获取验证码
    time.sleep(3)
    verify_code = check_code.get_check_code(phone_code_url)
    code_input = driver.find_element(By.XPATH, "//input[@data-smsloginreg='codeInput']")
    code_input.click()
    time.sleep(1)
    code_input.send_keys('123456')


    # 同意协议
    time.sleep(1)
    agree_tag = driver.find_element(By.CLASS_NAME, 'iconfont-agree')
    agree_tag.click()

    # 点击登陆
    time.sleep(1)
    login_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-smsloginreg='loginBtn']")))
    login_button.click()

    # 激活码
    time.sleep(1)
    exchange_module = WebDriverWait(driver, 5).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '.J_expCard'))
    )
    exchange_module.send_keys(exchange_code)

    # 点击激活
    time.sleep(1)
    active_button = driver.find_element(By.CLASS_NAME, 'J_submit')
    active_button.click()


if __name__ == '__main__':
    iqiyi_invoke('+86', '18633448636', '1', 'xxxx-1234-iqy-month1')